package com.bobi.tfca.ui.member

import android.os.Bundle
import android.widget.Toast
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentGroupAddMemberBinding
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.ui.adapter.GroupMemAddAdapter

class GroupAddMemberFragment : BaseFragment<FragmentGroupAddMemberBinding, GroupAddMemFragmentVM>(
    FragmentGroupAddMemberBinding::inflate,
    GroupAddMemFragmentVM::class.java,
    true
) {
    // 会员RV
    private lateinit var memberRV: RecyclerView

    override fun initFragment(
        binding: FragmentGroupAddMemberBinding,
        viewModel: GroupAddMemFragmentVM?,
        savedInstanceState: Bundle?
    ) {

        // 初始化标题
        val groupId = publicViewModel!!.chatRoom.value!!.groupId
        val groupName = publicViewModel!!.chatRoom.value!!.groupName


        // 初始化topPanel
        setTopPanelStyle(viewModel, binding, groupId, groupName)

        // 初始化RV
        initRecyclerView(viewModel, binding)

        // 添加按钮
        binding.addButton.setOnClickListener {
            val inputText = binding.searchBox.text.toString()
            val memberId = inputText.toIntOrNull()
            if (memberId != null) {
                viewModel?.getMemberById(publicViewModel!!, memberId, this)
            } else {
                Toast.makeText(requireContext(), "请输入有效ID", Toast.LENGTH_SHORT).show()
            }
        }

        // 设置viewmodel监听
        viewModel!!.members.observe(viewLifecycleOwner) {
            val adapter = memberRV.adapter as GroupMemAddAdapter
            adapter.memberResponseList = it
            adapter.notifyDataSetChanged()
        }

    }




    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(
        viewModel: GroupAddMemFragmentVM?,
        binding: FragmentGroupAddMemberBinding,
        groupId: Int,
        groupName: String?
    ) {
        val topPanel = binding.topPanel
        val leftImage = topPanel.leftImage
        leftImage.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        topPanel.rightButton.text = "确认"
        topPanel.rightButton.setOnClickListener {
            val adapter = memberRV.adapter as GroupMemAddAdapter
            val choseMemberList = adapter.memberResponseList
            if (choseMemberList != null && choseMemberList.size > 0) {
                val choseMemberIdList: List<Int> = choseMemberList.map { it.memberId }
                viewModel?.addMember(publicViewModel!!, groupId, choseMemberIdList, this)
            } else {
                Toast.makeText(requireActivity(), "请添加成员", Toast.LENGTH_SHORT).show()
            }
        }
        val title = topPanel.titleText
        // 群聊名称
        title.text = groupName
    }

    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        viewModel: GroupAddMemFragmentVM?,
        binding: FragmentGroupAddMemberBinding
    ) {
        // 初始化RV
        memberRV = binding.memberRV
        // 设置布局排列方式，默认垂直排列
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        memberRV.layoutManager = linearLayoutManager
        // 添加分隔线
        memberRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        // 绑定adapter
        val memberAdapter = GroupMemAddAdapter(this, arrayListOf())
        memberAdapter.setOnItemClickListener(object : GroupMemAddAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                // 点击按钮后取消添加
                val list : ArrayList<MemberResponse> = viewModel!!.members.value ?: ArrayList()
                if (list.isNotEmpty()) {
                    list.removeAt(position)
                    viewModel.members.value = list
                }
            }
        })
        memberRV.adapter = memberAdapter
    }
}